import { TableRowSelection } from 'antd/lib/table/interface'
import { TableState } from '@/components'

/**
 * 表格选择项配置
 * @param selectedRowKeys 选择项主键
 * @param setSelection 更新选择项方法
 * @param rowSelection 可选，已有的选择项
 */
export const useRowSelection = (
  selectedRowKeys: TableState['selectedRowKeys'],
  setSelection: TableState['setSelection'],
  rowSelection?: TableRowSelection
) => {
  /**
   * 覆盖选择行配置
   */
  const newRowSelection: TableRowSelection<any> = {
    ...rowSelection,
    selectedRowKeys: selectedRowKeys,
    onChange: (selectedRowKeys, selectedRows, info) => {
      setSelection(selectedRowKeys, selectedRows)
      rowSelection?.onChange?.(selectedRowKeys, selectedRows, info)
    },
  }

  return {
    rowSelection: newRowSelection,
  }
}
